package com.wayne.leetcode.problems;

public class Solution2409 {
  private static final int[] DAYS = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

  public static int countDaysTogether(String arriveAlice, String leaveAlice, String arriveBob, String leaveBob) {
    // 两个区间的交集算法 最大的左边 与 最小的右边 之差 + 1      如果差为负数 则不存在交集
    int left = getDays(arriveAlice.compareTo(arriveBob) > 0 ? arriveAlice : arriveBob);
    int right = getDays(leaveAlice.compareTo(leaveBob) < 0 ? leaveAlice : leaveBob);
    int days = Math.max(right - left + 1, 0);
    return days;
  }

  private static int getDays(String dateStr) {
    char[] chars = dateStr.toCharArray();
    int curMonth = (chars[0] - '0') * 10 + (chars[1] - '0') - 1;
    int curDays = (chars[3] - '0') * 10 + (chars[4] - '0');
    for (int i = 0; i < curMonth; i++) {
      curDays += DAYS[i];
    }
    return curDays;
  }

  public static void main(String[] args) {
    System.out.println(countDaysTogether("08-10", "09-20", "08-20", "10-01"));
  }
}